﻿using HCMS.CommonDtos;
using HCMS.Entities.OAuth2;
using HCMS.IRepositories.OAuth2;
using HCMS.IServices.OAuth2;
using HCMS.Repositories.OAuth2;
using HCMS.Utils;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace HCMS.Services.OAuth2
{
    /// <summary>
    /// Api 请求日志 应用服务
    /// </summary>
    public class ApiRequestLogsAppService : CommonAppService<OAuth2DbContext, ApiRequestLogs>, IApiRequestLogsAppService
    {
        private readonly IRepository<ApiRequestLogs> baseApiRequestLogsRepository;
        private readonly IApiRequestLogsRepository apiRequestLogsRepository;

        public ApiRequestLogsAppService(
            IRepository<ApiRequestLogs> baseApiRequestLogsRepository,
            IApiRequestLogsRepository apiRequestLogsRepository
            ) : base(baseApiRequestLogsRepository)
        {
            this.baseApiRequestLogsRepository = baseApiRequestLogsRepository;
            this.apiRequestLogsRepository = apiRequestLogsRepository;
        }

        /// <summary>
        /// 管理 列表
        /// </summary>
        /// <param name="searchArgs">搜索 参数</param>
        /// <param name="pager">分页 参数</param>
        /// <returns></returns>
        public async Task<ListPagerDto<object>> ManageList(SearchArgsDto searchArgs, PagerArgsDto pager)
        {
            var apiRequestLogs = await apiRequestLogsRepository.ManageList(searchArgs, pager);

            var result = new ListPagerDto<object>();
            result.PagerInfo = apiRequestLogs.PagerInfo;

            var data = new List<object>();
            foreach (var item in apiRequestLogs.Data)
            {
                data.Add(new
                {
                    id = item.Id,
                    createTime = item.CreateTime,
                    ip = item.Ip,
                    url = item.Url,
                    controllerName = item.ControllerName,
                    actionName = item.ActionName,
                    ExecutionTime = DateTimeUtil.ExecutionTimeStr(item.ExecutionTime),
                });
            }

            result.Data = data;

            return result;
        }
    }
}
